﻿package com.mui.controls
{
    import com.gengine.core.*;
    import com.gengine.game.*;
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import com.greensock.*;
    import com.mui.core.*;
    import com.mui.manager.*;
    import com.mui.utils.*;
    import fl.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.utils.*;

    public class GImageButton extends Button implements IToolTipItem, IFrUI
    {
        private var _isDisposed:Boolean;
        private var _isMouseOver:Boolean = false;
        private var _resName:String = "";
        protected var _toolTipData:Object;
        private var _reactionTime:int = 500;
        protected var _showTween:TweenMax;
        protected var _hideTween:TweenMax;
        private var _obj:Object;
        private var _filter:ColorMatrixFilter;
        private var _oldFilters:Array;
        private var _isRecordFilter:Boolean;
        private const _maxValue:int = 35;
        private const _maxValue2:int = 1;
        private static var _bitmapDataMap:Dictionary = new Dictionary();
        public static var _arySuffix:Array = [".png", ".jpg", ".jpeg", ".swf"];

        public function GImageButton()
        {
            this._oldFilters = [];
            this.buttonMode = true;
            this.useHandCursor = true;
            this.tabChildren = false;
            setStyle("overSkin", new Bitmap());
            setStyle("upSkin", new Bitmap());
            setStyle("downSkin", new Bitmap());
            setStyle("disabledSkin", new Bitmap());
            this.addEventListener(Event.ADDED_TO_STAGE, this.judgeToolTip);
            this.addEventListener(Event.REMOVED_FROM_STAGE, this.judgeToolTip);
            return;
        }// end function

        public function set upSkin(param1:String) : void
        {
            if (this._resName)
            {
                LoaderManager.instance.removeResourceEvent(this._resName, this.setStyles);
            }
            if (!param1)
            {
                setStyle("overSkin", new Bitmap());
                setStyle("upSkin", new Bitmap());
                setStyle("downSkin", new Bitmap());
                setStyle("disabledSkin", new Bitmap());
                this._resName = "";
                return;
            }
            this._resName = param1;
            LoaderManager.instance.load(this._resName, this.setStyles);
            return;
        }// end function

        protected function setStyles() : void
        {
            var _loc_1:* = ResourceManager.getInfoByName(getUrl(this._resName));
            if (!_loc_1)
            {
                return;
            }
            var _loc_2:* = _loc_1["bitmapData"] as BitmapData;
            setStyle("overSkin", _loc_2);
            setStyle("upSkin", _loc_2);
            setStyle("downSkin", _loc_2);
            setStyle("disabledSkin", _loc_2);
            return;
        }// end function

        public function get toolTipData()
        {
            return this._toolTipData;
        }// end function

        public function set toolTipData(param1) : void
        {
            this._toolTipData = param1;
            this.judgeToolTip();
            return;
        }// end function

        public function get reactionTime() : int
        {
            return this._reactionTime;
        }// end function

        public function set reactionTime(param1:int) : void
        {
            this._reactionTime = param1;
            return;
        }// end function

        protected function judgeToolTip(event:Event = null) : void
        {
            if (event && event.type == Event.ADDED_TO_STAGE && this.toolTipData || !event && this.toolTipData && Global.stage.contains(this))
            {
                ToolTipsManager.register(this);
            }
            else
            {
                ToolTipsManager.unregister(this);
            }
            this._filter = new ColorMatrixFilter();
            this._obj = new Object();
            this._obj.filter = this._filter;
            this._obj.value = 0;
            this._obj.value2 = 0;
            if (event && event.type == Event.REMOVED_FROM_STAGE && this._isMouseOver)
            {
                this._isMouseOver = false;
            }
            this.configEventListener(MouseEvent.MOUSE_OVER, this.showLight);
            this.configEventListener(MouseEvent.MOUSE_OUT, this.hideLight);
            this.configEventListener(MouseEvent.MOUSE_DOWN, this.hideLight);
            this.configEventListener(MouseEvent.MOUSE_UP, this.showLight);
            return;
        }// end function

        protected function showLight(event:MouseEvent) : void
        {
            if (this.enabled)
            {
                this._isMouseOver = true;
            }
            if (this._hideTween)
            {
                this._hideTween.kill();
            }
            if (!this._isRecordFilter)
            {
                this._oldFilters = this.filters;
                this._isRecordFilter = true;
            }
            this._obj.value = 0;
            this._obj.value2 = 1;
            this._showTween = TweenMax.to(this._obj, 0.3, {value:this._maxValue, value2:this._maxValue2, onUpdate:this.onEnterFrameHandler});
            return;
        }// end function

        protected function hideLight(event:MouseEvent) : void
        {
            this._isMouseOver = false;
            if (this._showTween)
            {
                this._showTween.kill();
            }
            this._obj.value = this._maxValue;
            this._obj.value2 = this._maxValue2;
            this._hideTween = TweenMax.to(this._obj, 0.3, {value:0, value2:1, onUpdate:this.onEnterFrameHandler, onComplete:this.onComplete});
            return;
        }// end function

        private function onEnterFrameHandler() : void
        {
            var _loc_1:Array = [];
            _loc_1 = _loc_1.concat(this._oldFilters);
            this._filter.matrix = [this._obj.value2, 0, 0, 0, this._obj.value, 0, this._obj.value2, 0, 0, this._obj.value, 0, 0, this._obj.value2, 0, this._obj.value, 0, 0, 0, this._obj.value2, 0];
            _loc_1.push(this._filter);
            this.filters = _loc_1;
            return;
        }// end function

        private function onComplete() : void
        {
            this.filters = this._oldFilters;
            this._isRecordFilter = false;
            return;
        }// end function

        public function configEventListener(param1:String, param2:Function, param3:Boolean = false, param4:int = 0, param5:Boolean = false) : void
        {
            ObjEventListerTool.addObjEvent(this, param1, param2, param3);
            addEventListener(param1, param2, param3, param4, param5);
            return;
        }// end function

        public function set isDisposed(param1:Boolean) : void
        {
            this._isDisposed = param1;
            return;
        }// end function

        override public function removeEventListener(param1:String, param2:Function, param3:Boolean = false) : void
        {
            ObjEventListerTool.removeObjEvent(this, param1, param2, param3);
            super.removeEventListener(param1, param2, param3);
            return;
        }// end function

        public function dispose(param1:Boolean = true) : void
        {
            var _loc_3:int = 0;
            var _loc_4:DisplayObject = null;
            if (this._isDisposed && param1)
            {
                if (Global.isDebugModle)
                {
                    Alert.show("存在组件二次释放" + this.toString());
                }
                return;
            }
            if (this._resName)
            {
                LoaderManager.instance.removeResourceEvent(this._resName, this.setStyles);
                this._resName = "";
            }
            clearStyle("overSkin");
            clearStyle("upSkin");
            clearStyle("disabledSkin");
            clearStyle("downSkin");
            ObjEventListerTool.delObjEvent(this);
            var _loc_2:* = this.numChildren;
            _loc_3 = _loc_2 - 1;
            while (_loc_3 >= 0)
            {
                
                _loc_4 = this.getChildAt(_loc_3);
                if (_loc_4 is IDispose)
                {
                    this.removeChild(_loc_4);
                    (_loc_4 as IFrUI).dispose(param1);
                }
                _loc_3 = _loc_3 - 1;
            }
            if (this.parent)
            {
                this.parent.removeChild(this);
            }
            if (param1)
            {
                UICompomentPool.disposeUICompoment(this);
            }
            this._reactionTime = 500;
            return;
        }// end function

        public static function getUrl(param1:String) : String
        {
            var _loc_3:String = null;
            var _loc_2:* = param1.toLocaleLowerCase();
            for each (_loc_3 in _arySuffix)
            {
                
                if (_loc_2.indexOf(_loc_3) > 0)
                {
                    return param1;
                }
            }
            return param1 + ".swf";
        }// end function

    }
}
